package bb.lanxing.lib.devices.sprint.model.impl;

import android.util.Pair;
import bb.lanxing.lib.devices.sprint.SprintFile;
import bb.lanxing.lib.devices.sprint.entity.SprintNav;
import bb.lanxing.lib.devices.sprint.model.SprintNavigationModel;
import bb.lanxing.lib.devices.sprint.utils.SprintFileHelper;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.functions.FuncN;
import rx.schedulers.Schedulers;
import rx.subjects.BehaviorSubject;

public abstract class AbstractSprintNavigationModel implements SprintNavigationModel {
    private List<SprintNav> sprintNavList;
    protected final BehaviorSubject<List<SprintNav>> lushuSubject = BehaviorSubject.create();
    protected final BehaviorSubject<Pair<String, Float>> progressUpdateSubject = BehaviorSubject.create();
    protected final BehaviorSubject<Pair<Long, Integer>> navigationStatusSubject = BehaviorSubject.create();

    protected abstract Observable<SprintNav> loadSprintNav(long j);

    @Override
    public void subscribeLushu(Subscriber<List<SprintNav>> subscriber) {
        this.lushuSubject.observeOn(AndroidSchedulers.mainThread()).subscribe((Subscriber<? super List<SprintNav>>) subscriber);
    }

    @Override
    public void subscribeProgressUpdate(Subscriber<Pair<String, Float>> subscriber) {
//        this.progressUpdateSubject.buffer(500L, TimeUnit.MILLISECONDS).observeOn(AndroidSchedulers.mainThread()).flatMap(new Func1<List<Pair<String, Float>>, Observable<Pair<String, Float>>>() {
//            @Override
//            public Observable<Pair<String, Float>> call(List<Pair<String, Float>> list) {
//                return (list == null || list.isEmpty()) ? Observable.empty() : Observable.just(list.get(list.size() - 1));
//            }
//        }).subscribe((Subscriber<? super R>) subscriber);
    }

    @Override
    public void subscribeNavigationStatus(Subscriber<Pair<Long, Integer>> subscriber) {
        this.navigationStatusSubject.observeOn(AndroidSchedulers.mainThread()).subscribe((Subscriber<? super Pair<Long, Integer>>) subscriber);
    }

    public Observable<List<SprintNav>> loadNavFromFile(final String str) {
        return Observable.just(str).subscribeOn(Schedulers.io()).flatMap(new Func1<String, Observable<List<SprintNav>>>() {
            @Override
            public Observable<List<SprintNav>> call(String str2) {
                List<SprintFile> parsetNavList = SprintFileHelper.parsetNavList(str);
                if (parsetNavList == null || parsetNavList.isEmpty()) {
                    return Observable.just(Collections.emptyList());
                }
                ArrayList arrayList = new ArrayList();
                for (SprintFile sprintFile : parsetNavList) {
                    if (sprintFile.getSize() > 0) {
                        try {
                            String fileName = sprintFile.getFileName();
                            arrayList.add(AbstractSprintNavigationModel.this.loadSprintNav(Long.parseLong(fileName.substring(0, fileName.indexOf(SprintFileHelper.NAV_FILE_STUFFIX)))));
                        } catch (NumberFormatException unused) {
                        }
                    }
                }
                if (arrayList.isEmpty()) {
                    return Observable.just(Collections.emptyList());
                }
                return Observable.zip(arrayList, new FuncN<List<SprintNav>>() {
                    @Override
                    public List<SprintNav> call(Object... objArr) {
                        ArrayList arrayList2 = new ArrayList();
                        for (Object obj : objArr) {
                            if (obj instanceof SprintNav) {
                                arrayList2.add((SprintNav) obj);
                            }
                        }
                        return new ArrayList(arrayList2);
                    }
                });
            }
        });
    }

    protected Long getNavServerIdByNavName(String str) {
        try {
            return Long.parseLong(str.substring(0, str.indexOf(SprintFileHelper.NAV_FILE_STUFFIX)));
        } catch (NumberFormatException unused) {
            return null;
        }
    }

    protected long getNavIdByName(String str) {
        Long navId;
        Long navServerIdByNavName = getNavServerIdByNavName(str);
        if (navServerIdByNavName == null || (navId = getNavByServerId(navServerIdByNavName).getNavId()) == null) {
            return -1L;
        }
        return navId;
    }

    public SprintNav getNavByName(String str) {
        Long navServerIdByNavName = getNavServerIdByNavName(str);
        if (navServerIdByNavName != null) {
            return getNavByServerId(navServerIdByNavName);
        }
        return null;
    }

    protected SprintNav getNavByServerId(long j) {
        List<SprintNav> list = this.sprintNavList;
        if (list != null) {
            for (SprintNav sprintNav : list) {
                Long navServerId = sprintNav.getNavServerId();
                if (navServerId != null && navServerId == j) {
                    return sprintNav;
                }
            }
            return null;
        }
        return null;
    }

    protected SprintNav getNavById(long j) {
        List<SprintNav> list = this.sprintNavList;
        if (list != null) {
            for (SprintNav sprintNav : list) {
                if (sprintNav.getNavId() != null && sprintNav.getNavId() == j) {
                    return sprintNav;
                }
            }
            return null;
        }
        return null;
    }

    @Override
    public long getNavServerId(long j) {
        Long navId = getNavById(j).getNavId();
        if (navId != null) {
            return navId;
        }
        return -1L;
    }

    @Override
    public long getNavId(long j) {
        SprintNav navByServerId = getNavByServerId(j);
        Long navId = navByServerId.getNavId();
        if (navByServerId != null) {
            return navId;
        }
        return -1L;
    }

    protected SprintNav first() {
        List<SprintNav> list = this.sprintNavList;
        if (list == null || list.isEmpty()) {
            return null;
        }
        return this.sprintNavList.get(0);
    }

    @Override
    public boolean isExists(long j) {
        return getNavByServerId(j) != null;
    }

    public void setSprintNavList(List<SprintNav> list) {
        this.sprintNavList = list;
        this.lushuSubject.onNext(list);
    }

    @Override
    public void clear() {
        List<SprintNav> list = this.sprintNavList;
        if (list != null) {
            list.clear();
        }
    }

    @Override
    public void release() {
        BehaviorSubject<Pair<String, Float>> behaviorSubject = this.progressUpdateSubject;
        if (behaviorSubject != null) {
            behaviorSubject.onCompleted();
        }
        BehaviorSubject<Pair<Long, Integer>> behaviorSubject2 = this.navigationStatusSubject;
        if (behaviorSubject2 != null) {
            behaviorSubject2.onCompleted();
        }
        BehaviorSubject<List<SprintNav>> behaviorSubject3 = this.lushuSubject;
        if (behaviorSubject3 != null) {
            behaviorSubject3.onCompleted();
        }
    }
}
